package study.ws.area.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**  
* @Name: JDBCUtil
* @Description: 数据库jdbc连接工具类
* @Author: 李超
* @Create Date: 2017-01-07
*/
public class JDBCUtil {
	
	private final static String DBDRIVER = "com.mysql.jdbc.Driver" ;
	private final static String DBURL = "jdbc:mysql://127.0.0.1:3306/db_ws" ;
	private final static String DBUSER = "root" ;
	private final static String DBPASSWORD = "P@ssw0rd" ;
	
	static {
		try {
			Class.forName(DBDRIVER) ;
		} catch (ClassNotFoundException e) {
			throw new ExceptionInInitializerError(e) ;
		}
	}
	
	public static Connection getConnection() {
		Connection con = null ;
		try {
			con = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD) ;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con ;
	}
	
	public static void close(Connection con, PreparedStatement ps, ResultSet rs) {
		if(rs != null) {
			try {
				rs.close() ;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				rs = null ;
			}
		}
		if(ps != null) {
			try {
				ps.close() ;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				ps = null ;
			}
		}
		if(con != null) {
			try {
				con.close() ;
			} catch (SQLException e) {
				e.printStackTrace();
			} finally {
				con = null ;
			}
		}
	}

}
